local skel = fk.CreateSkill {
  name = "emo__wenyun",
  dynamic_desc = function (self, player, lang)
    local mark = player:getMark(self.name)
    if mark == 0 then mark = {1, 2, 3} end
    return "emo__wenyun_dyn:" .. mark[1] .. ":" .. mark[2] .. ":" .. mark[3]
  end,
}

Fk:loadTranslationTable{
  ["emo__wenyun"] = "温蕴",
  [":emo__wenyun"] = "出牌阶段限一次，你可以令一名角色执行一项：弃置一张牌；重铸两张牌；获得三张【影】。"..
  "每当你造成或受到伤害后，你可以选择以上两项令你与另一方执行，然后交换两项的数字。",

  [":emo__wenyun_dyn"] = "出牌阶段限一次，你可以令一名角色执行一项：弃置{1}张牌；重铸{2}张牌；获得{3}张【影】。"..
  "每当你造成或受到伤害后，你可以选择以上两项令你与另一方执行，然后交换两项的数字。",

  ["#emo__wenyun"] = "温蕴：选一项，令一名角色执行",
  ["#emo__wenyun-recast"] = "温蕴：请重铸 %arg 张牌",
  ["#emo__wenyun-invoke"] = "温蕴：你可以令 %src 与 %dest 执行你选择的两项",
  ["#emo__wenyun-self"] = "温蕴：选择一项令自己执行",
  ["#emo__wenyun-choice"] = "温蕴：选择一项令 %src 执行",

  ["emo__wenyun_discard"] = "弃置%arg张牌",
  ["emo__wenyun_recast"] = "重铸%arg张牌",
  ["emo__wenyun_shade"] = "获得%arg张【影】",
}

local U = require "packages.utility.utility"

local function doWenyun(to, choice)
  local room = to.room
  if to.dead then return end
  local num = tonumber(choice:split(":")[4])---@type integer
  if choice:startsWith("emo__wenyun_discard") then
    room:askToDiscard(to, {min_num = num, max_num = num, include_equip = true, skill_name = skel.name, cancelable = false})
  elseif choice:startsWith("emo__wenyun_recast") then
    local cards = to:getCardIds("he")
    if #cards == 0 then return end
    if #cards > num then
      cards = room:askToCards(to, {
        min_num = num, max_num = num, skill_name = skel.name, cancelable = false, include_equip = true,
        prompt = "#emo__wenyun-recast:::" .. num
      })
    end
    room:recastCard(cards, to, skel.name)
  else
    room:obtainCard(to, U.getShade(room, num), true, fk.ReasonPrey, to, skel.name)
  end
end


skel:addEffect("active", {
  anim_type = "drawcard",
  interaction = function(self, player)
    local mark = player:getMark(skel.name)
    if mark == 0 then mark = {1, 2, 3} end
    local choices = {"emo__wenyun_discard:::".. mark[1], "emo__wenyun_recast:::".. mark[2], "emo__wenyun_shade:::".. mark[3]}
    return UI.ComboBox { choices = choices }
  end,
  card_num = 0,
  target_num = 1,
  target_filter = function (self, player, to_select, selected)
    if self.interaction.data and not self.interaction.data:startsWith("emo__wenyun_shade") and to_select:isNude() then return false end
    return #selected == 0
  end,
  times = function (self, player)
    return 1 - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local to = effect.tos[1]
    local choice = self.interaction.data
    doWenyun(to, choice)
  end,
})

local can_trigger = function (self, event, target, player, data)
  return target == player and player:hasSkill(skel.name) and data.from and data.to and data.from:isAlive() and data.to:isAlive()
end

local on_cost = function (self, event, target, player, data)
  local room = player.room
  if not room:askToSkillInvoke(player, {skill_name = skel.name,
    prompt = "#emo__wenyun-invoke:"..data.from.id..":"..data.to.id }) then return false end
  local mark = player:getMark(skel.name)
  if mark == 0 then mark = {1, 2, 3} end
  local cost_data = { choice_list = {} }
  local all_choices = {"emo__wenyun_discard:::".. mark[1], "emo__wenyun_recast:::".. mark[2], "emo__wenyun_shade:::".. mark[3]}
  local choice = room:askToChoice(player, {choices = all_choices, skill_name = skel.name, prompt = "#emo__wenyun-self" })
  table.insert(cost_data.choice_list, {player, choice})
  local choices = table.simpleClone(all_choices)
  table.removeOne(choices, choice)
  local other = data.from == player and data.to or data.from
  choice = room:askToChoice(player, {choices = choices, skill_name = skel.name, all_choices = all_choices,
  prompt = "#emo__wenyun-choice:"..other.id })
  table.insert(cost_data.choice_list, {other, choice})
  cost_data.tos = {other}
  event:setCostData(self, cost_data)
  return true
end

local on_use = function (self, event, target, player, data)
  local marks = {}
  for _, list in ipairs(event:getCostData(self).choice_list) do
    doWenyun(list[1], list[2])
    local splitter = list[2]:split(":")
    table.insert(marks, {splitter[1], tonumber(splitter[4]) })
  end
  if player.dead then return end
  local mark1, mark2 = marks[1], marks[2]
  marks = { {mark1[1], mark2[2]}, {mark2[1], mark1[2]} }
  local mark = player:getMark(skel.name)
  if mark == 0 then mark = {1, 2, 3} end
  player:setMark(skel.name, marks)
  local all_choices = {"emo__wenyun_discard", "emo__wenyun_recast", "emo__wenyun_shade"}
  for _, v in ipairs(marks) do
    local index = table.indexOf(all_choices, v[1])
    mark[index] = v[2]
  end
  player.room:setPlayerMark(player, skel.name, mark)
end

skel:addEffect(fk.Damage, {
  anim_type = "offensive",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addLoseEffect(function (self, player)
  player.room:setPlayerMark(player, skel.name, 0)
end)

return skel
